package com.itheima.filter;

import com.itheima.bean.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 祺诺
 * @Name; xqf
 * @Date: 2020/12/28 14:39
 */
@WebFilter("/*")
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        String uri = req.getRequestURI();
        if (uri.endsWith("login.jsp")||uri.endsWith("LoginStudentServlet")){
            filterChain.doFilter(req,resp);
            return;
        }else{
            User user = (User) req.getSession().getAttribute("user");
            if(user!=null){
                filterChain.doFilter(req,resp);
                return;
            }else{
                resp.sendRedirect(req.getContextPath()+"/login.jsp");
            }
        }
    }
}
